package com.ease.framework.gef4p.viewers {
   import com.ease.util.CursorManagerExt;
   import com.ease.component.controls.HandlesMouseCursors;
   import com.ease.component.managers.DepthManager;
   import com.ease.common.models.Model;
   import com.ease.framework.figures.IContainerFigure;
   import com.ease.framework.figures.IFigure;
   import com.ease.framework.flex.controls.Handle;
   import com.ease.framework.gef.IEditPart;
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef.events.GraphicalBehaviorEvent;
   import com.ease.framework.gef.events.ModelEvent;
   import com.ease.framework.gef.ui.ISelection;
   import com.ease.framework.gef.ui.SelectionManager;
   import com.ease.framework.gef4g.GraphicalBehavior;
   import com.ease.framework.gef4g.GraphicalViewerControl;
   import com.ease.framework.gef4g.GraphicalViewport;
   import com.ease.framework.gef4g.IGraphicalViewer;
   import com.ease.framework.gef4g.IGraphicalViewerControl;
   import com.ease.framework.gef4p.actions.HorizontalAlignButton;
   import com.ease.framework.gef4p.actions.VerticalAlignButton;
   import com.ease.framework.gef4p.constant.DiagramConstant;
   import com.ease.framework.gef4p.editparts.EdgeEditPart;
   import com.ease.framework.gef4p.figures.IArtifactFigure;
   import com.ease.framework.gef4p.figures.IDraggingFigure;
   import com.ease.framework.gef4p.figures.IFigureResizable;
   import com.ease.framework.gef4p.figures.ILabelFigure;
   import com.ease.framework.gef4p.figures.ILaneFigure;
   import com.ease.framework.gef4p.figures.INodeFigure;
   import com.ease.framework.gef4p.figures.IVertexFigure;
   import com.ease.framework.gef4p.figures.edge.IEdgeAnchor;
   import com.ease.framework.gef4p.figures.edge.IEdgeFigure;
   import com.ease.framework.gef4p.figures.impl.AnnotationFigure;
   import com.ease.framework.gef4p.figures.impl.AnnotationLabelFigure;
   import com.ease.framework.gef4p.figures.impl.ArtifactFigure;
   import com.ease.framework.gef4p.figures.impl.FigureWithHandles;
   import com.ease.framework.gef4p.figures.impl.NodeFigure;
   import com.ease.framework.gef4p.figures.impl.edge.Bend;
   import com.ease.framework.gef4p.figures.impl.edge.EdgeFigure;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.gef4p.model.Vertex;
   import com.ease.framework.layers.LabelLayer;
   import com.ease.framework.layers.LaneLayer;
   import com.ease.framework.layers.Layer;
   import com.ease.framework.layers.PrimaryLayer;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.ICommand;
   import com.ease.framework.workbench.spi.impls.CompoundCommand;
   
   import flash.display.DisplayObject;
   import flash.events.KeyboardEvent;
   import flash.events.MouseEvent;
   import flash.geom.Point;
   import flash.ui.Keyboard;
   
   import mx.collections.ArrayCollection;
   import mx.containers.Canvas;
   import mx.controls.scrollClasses.ScrollThumb;
   import mx.core.Container;
   import mx.core.IToolTip;
   import mx.core.UIComponent;
   import mx.events.DragEvent;

   public class DiagramGraphicalViewerControl extends GraphicalViewerControl implements IGraphicalViewerControl {

      //这个主要是临时EdgeFigure，在刚开始绘制过程中，会先创建一个临时Edge.
      protected var currentDrawingEdge:EdgeFigure;

      //这个主要用于记录当前正在移动的Edge Figure的原始Edge Figure
      private var currentMovingOriginalEdgeFigure:EdgeFigure;

      //Mask阴影. 放置于 Mask Layer
      private var mask_cvs:Canvas = new Canvas();

      /* *********************************************************************************************
       *
       * 定义移动Figure的属性
       *
       * ********************************************************************************************/
      protected var localClickPoint:Point = new Point();
      protected var localClickAngle:Number = 0;

      protected var isCorner:Boolean = false;
      protected var isResizingDown:Boolean = false;
      protected var isResizingRight:Boolean = false;
      protected var isResizingLeft:Boolean = false;
      protected var isResizingUp:Boolean = false;
      protected var isMoving:Boolean = false;

      protected var originalPosition:Point = new Point();
      protected var originalSize:Point = new Point();

      protected var wasMoved:Boolean = false;
      protected var wasResized:Boolean = false;

      //用于基于在移动LaneFigure时候，目前所对应的ILaneFigure对象
      protected var targetLocationLaneFigure:ILaneFigure = null;
      //用于记录是放在当前目标Lane的前面还是后面
      protected var targetLocationLaneFigure_before:Boolean = false;

      protected var tt:IToolTip;

      public function DiagramGraphicalViewerControl() {
         super();

         this.addEventListener(DragEvent.DRAG_ENTER, onDragEnter);
         this.addEventListener(DragEvent.DRAG_DROP, onDragDrop);
         this.addEventListener(DragEvent.DRAG_OVER, onDragOver);
         this.addEventListener(DragEvent.DRAG_EXIT, onDragExit);
         this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
         this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
         this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
         this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
         this.addEventListener(MouseEvent.CLICK, onMouseClick);
         this.addEventListener(MouseEvent.DOUBLE_CLICK, onMouseDoubleClick);
         this.addEventListener(ModelEvent.ModelEvent_SELECTED, onModelSelect);
         this.addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
         this.doubleClickEnabled = true;

         //要使Canvas响应key事件，则必须预先通过 setFocus 设置焦点
         this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

         mask_cvs.name = "Graphical_Mask";
         mask_cvs.visible = false;
         mask_cvs.setStyle("backgroundAlpha", 0.2);
         mask_cvs.setStyle("backgroundColor", 0xA6CCFF);
         mask_cvs.setStyle("borderStyle", "solid");
         mask_cvs.setStyle("borderColor", 0x379BE3);
         mask_cvs.setStyle("borderThickness", 0.6);
      }

//=========================================================================
//
// 响应键盘事件.
//
//=========================================================================
      protected function onKeyDown(event:KeyboardEvent):void {
         if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT) {
            handleMovePress(event.keyCode, event.shiftKey);
         }

         if (event.keyCode == Keyboard.DELETE)
            handleDeletePress();

         if (event.keyCode == 90 || event.keyCode == 89)
            handleCommondPress(event.keyCode, event.ctrlKey);

         if (event.keyCode == 67 || event.keyCode == 86 || event.keyCode == 88)
            handleClipBoardPress(event.keyCode, event.ctrlKey)

         if (event.keyCode == 83 && event.ctrlKey)
            handleSavePress();
         if (event.keyCode == Keyboard.ENTER)
            handleEnterPress();

         if (event.keyCode == 67 && !event.ctrlKey)
            handleEdgePress();

         if (event.keyCode == 50 && event.ctrlKey)
            handleHorizontalAlignPress();

         if (event.keyCode == 49 && event.ctrlKey)
            handleVerticalAlignPress();

         if (event.keyCode == 65 && event.ctrlKey)
            handleSelectPress();

         setFocus();
      }

      public function handleDeletePress():void {
         var selections:Array = getSelections();
         if (selections.length == 0) {
            return;
         }

         var command:CompoundCommand = new CompoundCommand();
         for each (var selection:IFigure in selections) {
            if (selection is EdgeFigure) {
               var aep:IEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(selection);
               if (aep != null) {
                  var reCommand:ICommand = aep.createRemoveCommand();
                  command.addCommand(reCommand);
               }
            }
         }

         for each (selection in selections) {
            if (selection is NodeFigure) {
               aep = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(selection);
               if (aep != null) {
                  var rnCommand:ICommand = aep.createRemoveCommand();
                  command.addCommand(rnCommand);
               }
            }

            if (selection is AnnotationFigure) {
               aep = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(selection);
               if (aep != null) {
                  var raCommand:ICommand = aep.createRemoveCommand();
                  command.addCommand(raCommand);
               }
            }
         }
         IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().push(command);
      }

      protected function handleMovePress(keyCode:uint, shiftKeyDown:Boolean):void {
         var selections:Array = getSelections();
         if (selections.length == 0) {
            return;
         }
         var command:CompoundCommand = new CompoundCommand();

         var space:Number = 0;

         var newPointGAP_x:Number = 0;
         var newPointGAP_y:Number = 0;

         if (shiftKeyDown) {
            space = 1;
         }
         else {
            space = 12;
         }

         switch (keyCode) {
            case Keyboard.UP:
               newPointGAP_y = newPointGAP_y - space;
               break;
            case Keyboard.DOWN:
               newPointGAP_y = newPointGAP_y + space;
               break;
            case Keyboard.LEFT:
               newPointGAP_x = newPointGAP_x - space;
               break;
            case Keyboard.RIGHT:
               newPointGAP_x = newPointGAP_x + space;
               break;
         }

         //刷新ScrollBar
         //判断当前Drag对象是否已经到达边线.
         var maxRightLocation:Number = 0;
         var maxBottomLocation:Number = 0;

         for each (var selection:IFigure in selections) {
            if (selection is IVertexFigure) {
               if (selection.x + newPointGAP_x <= 5)
                  continue;
               if (selection.y + newPointGAP_y <= 5)
                  continue;

               var aep:IEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(selection);
               if (aep.model is Vertex) {

                  var mrCommand:ICommand = aep.createMoveResizeCommand(newPointGAP_x, newPointGAP_y, 0, 0);
                  command.addCommand(mrCommand);
               }

               if (maxRightLocation < selection.x + selection.width) {
                  maxRightLocation = selection.x + selection.width;
               }
               if (maxBottomLocation < selection.y + selection.height) {
                  maxBottomLocation = selection.y + selection.height;
               }
            }
         }
         IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().push(command);

         this.refreshScrollBar(maxRightLocation, maxBottomLocation);

      }

      protected function handleCommondPress(keyCode:uint, ctrlKeyDown:Boolean):void {
         if (!ctrlKeyDown)
            return;

         if (keyCode == 90)
            IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().undo();
         if (keyCode == 89)
            IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().redo();
      }

      protected function handleClipBoardPress(keyCode:uint, ctrlKeyDown:Boolean):void {
         if (!ctrlKeyDown)
            return;

         switch (keyCode) {
            case 67: //copy
               copyObject();
               break;
            case 86: //past
               pastObject();
               break;
            case 88: //cut
               cutObject();
               break;
            default:
               break;
         }
      }

      protected function copyObject():void {
      }

      protected function pastObject():void {
      }

      protected function cutObject():void {
         var selections:Array = getSelections();
         if (selections.length == 0) {
            return;
         }
      }

      protected function handleEnterPress():void {
         var selections:Array = getSelections();
         if (selections.length == 0) {
         }
         else if (selections.length == 1) {
            handleFigureProperty(selections.pop() as IFigure);
         }
         else
            return;
      }

      protected function handleSavePress():void {

      }

      protected function clipboradChanged():void {

      }

      protected function handleHorizontalAlignPress():void {
         var event:MouseEvent = new MouseEvent(MouseEvent.CLICK);
         var horizontalAlign:HorizontalAlignButton = new HorizontalAlignButton();
         horizontalAlign.dispatchEvent(event);
      }

      protected function handleVerticalAlignPress():void {
         var event:MouseEvent = new MouseEvent(MouseEvent.CLICK);

         var verticalAlign:VerticalAlignButton = new VerticalAlignButton();
         verticalAlign.dispatchEvent(event);
      }

      protected function handleSelectPress():void {
         var figures:Array = IEditor(Workbench.instance.currentEditor).getEditDomain().getAllFigures();

         if (figures.length == 0)
            return;
         var sm:SelectionManager = IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager();

         for each (var figure:IFigure in figures) {
            if (figure is IEdgeFigure)
               continue;

            if (figure is ISelection)
               sm.addSelected(ISelection(figure));
         }
      }

      protected function handleEdgePress():void {
         IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType = GraphicalBehavior.Behavior_CreateEdge;

         dispatchEvent(new GraphicalBehaviorEvent(GraphicalBehaviorEvent.BEHAVIOR_CHANGED, true));
      }

//========================================================================

      protected function onDragEnter(event:DragEvent):void {
      }

      protected function onDragOver(event:DragEvent):void {
      }

      protected function onDragDrop(event:DragEvent):void {
      }

      protected function onDragExit(event:DragEvent):void {
      }

      /**
       * MouseDown 主要响应 选择、拖拽 操作.
       * @param event
       *
       */
      protected function onMouseDown(event:MouseEvent):void {
         var currentZoomNum:Number = IGraphicalViewer(this.getViewer()).getZoomCurrentNumber();

         originalSize.x = width;
         originalSize.y = height;
         originalPosition.x = event.stageX;
         originalPosition.y = event.stageY;

         var hscrollnum:Number = 0;
         var vscrollnum:Number = 0;
         
         if (event.target is ScrollThumb) {
            return;
         }

         if (horizontalScrollBar)
            hscrollnum = horizontalScrollBar.scrollPosition;

         if (verticalScrollBar)
            vscrollnum = verticalScrollBar.scrollPosition;

         //获取鼠标所放置位置的Figure图形.
         var targetFigure:IFigure = findFigureAt(event.stageX, event.stageY);

         if (targetFigure is IEdgeAnchor) {
            if (IEdgeAnchor(targetFigure).getLocationType() == DiagramConstant.EDGE_ANCHOR_LOCATION_SOURCE) {
               setBehaviorType(GraphicalBehavior.Behavior_MoveEdgeAnchor_Source);
               dispatchEvent(new GraphicalBehaviorEvent(GraphicalBehaviorEvent.BEHAVIOR_CHANGED, true));
            }
            else {
               setBehaviorType(GraphicalBehavior.Behavior_MoveEdgeAnchor_Target);
               dispatchEvent(new GraphicalBehaviorEvent(GraphicalBehaviorEvent.BEHAVIOR_CHANGED, true));
            }

            //显示用于拖拽用的currentDrawingEdge
            currentMovingOriginalEdgeFigure = EdgeFigure(IEdgeAnchor(targetFigure).getEdge());

            if (currentDrawingEdge == null) {
               currentDrawingEdge = EdgeFigure(IEditor(Workbench.instance.currentEditor).getFigureFactory().createTempEdgeFigure());
               currentDrawingEdge.name = "templateLine";
               currentDrawingEdge.visible = false;
               this.addChild(currentDrawingEdge);
            }
            var sourceBend:Bend = currentMovingOriginalEdgeFigure.getSourceBend();
            if (sourceBend != null) {
               currentDrawingEdge.setSourceBend(sourceBend.x * currentZoomNum, sourceBend.y * currentZoomNum);
            }
            var targetBend:Bend = currentMovingOriginalEdgeFigure.getTargetBend();
            if (targetBend != null) {
               currentDrawingEdge.setTargetBend(targetBend.x * currentZoomNum, targetBend.y * currentZoomNum);
            }
            currentDrawingEdge.visible = true;

            return;
         }

         if (targetFigure is ILabelFigure) {
            targetFigure = targetFigure.parent as IFigure;
         }

         if (targetFigure is IEdgeFigure) {

//    			IEdgeFigure(targetFigure).clearBends();
            if (!ISelection(targetFigure).isSelected)
               pushFigureIntoSelectionManager(ISelection(targetFigure), event);

            if (getBehaviorType() == GraphicalBehavior.Behavior_CreateEdge) {
               setBehaviorType(GraphicalBehavior.Behavior_Select);
               dispatchEvent(new GraphicalBehaviorEvent(GraphicalBehaviorEvent.BEHAVIOR_CHANGED, true));
            }
         }

         //点中空白区域.
         if (targetFigure is IContainerFigure) {

            IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager().clearSelected();

            if (IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType == GraphicalBehavior.Behavior_CreateEdge) {
               IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType = GraphicalBehavior.Behavior_Select;
               dispatchEvent(new GraphicalBehaviorEvent(GraphicalBehaviorEvent.BEHAVIOR_CHANGED, true));
            }

            //此时进行Mask Select行为
            var maskLayer:Layer = getLayer(GraphicalViewport.LAYER_MASK); //  this.getViewer().getViewport().maskLayer;
            if (maskLayer.getChildByName("Graphical_Mask") == null) {
               maskLayer.addChild(mask_cvs);
            }
            var maskPoint:Point = maskLayer.globalToLocal(new Point(event.stageX, event.stageY));
            mask_cvs.x = maskPoint.x;
            mask_cvs.y = maskPoint.y;
            mask_cvs.width = 0;
            mask_cvs.height = 0;
            mask_cvs.visible = true;

            IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType = GraphicalBehavior.Behavior_Marquee;
            return;
         }

         if (targetFigure is ISelection && !ISelection(targetFigure).isSelected && !event.shiftKey) {
            pushFigureIntoSelectionManager(ISelection(targetFigure), event);
         }

         if (targetFigure is INodeFigure) {

            if (IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType == GraphicalBehavior.Behavior_CreateEdge) {

               if (currentDrawingEdge == null) {
                  currentDrawingEdge = EdgeFigure(IEditor(Workbench.instance.currentEditor).getFigureFactory().createTempEdgeFigure());
                  currentDrawingEdge.name = "templateLine";
                  currentDrawingEdge.visible = false;
                  this.addChild(currentDrawingEdge);
               }

               currentDrawingEdge.source = targetFigure as INodeFigure;
               currentDrawingEdge.graphics.clear();

               var point:Point = new Point(event.stageX, event.stageY);
               point = globalToLocal(point);

               currentDrawingEdge.setSourceBend(point.x + hscrollnum, point.y + vscrollnum);
               currentDrawingEdge.setTargetBend(point.x + hscrollnum, point.y + vscrollnum);
               currentDrawingEdge.visible = true;
               return;

            }
         }

         //判断此时指针放置于Figure Handle上.
         if (targetFigure is ISelection) {
            //setMouseCursor(event.stageX,event.stageY,FigureWithHandles(targetFigure));

            var selectedHandle:Handle;

            if (targetFigure is IFigureResizable) {

               if (targetFigure is FigureWithHandles) {

                  if (IFigureResizable(targetFigure).allowRisize) {
                     for each (var handle:Handle in FigureWithHandles(targetFigure).getHandles()) {
                        if (handle.hitTestPoint(event.stageX, event.stageY)) {
                           cursorManager.removeCursor(cursorManager.currentCursorID);
                           cursorManager.setCursor(HandlesMouseCursors.getCursor(handle.getCursorName()), 2, -5, -8);

                           isCorner = handle.isCorner();
                           isResizingDown = handle.resizeDown;
                           isResizingLeft = handle.resizeLeft;
                           isResizingRight = handle.resizeRight;
                           isResizingUp = handle.resizeUp;

                           selectedHandle = handle;

                           break;
                        }
                     }
                  }

               }
            }

            if (selectedHandle == null && targetFigure is IVertexFigure && IVertexFigure(targetFigure).allowDrag) {
               isMoving = true;
               cursorManager.removeCursor(cursorManager.currentCursorID);
               cursorManager.setCursor(HandlesMouseCursors.getCursor("SizeAll"), 2, -11, -13);
            }

         }

         //将图形的替身放置于Dragging Layer
         for each (var iFigure:IFigure in getSelections()) {
            if (iFigure is IVertexFigure) {
               this.getLayer(GraphicalViewport.LAYER_DRAGGING).addChild(IVertexFigure(iFigure).getDraggingFigure() as DisplayObject);
            }
         }

         event.stopPropagation();
      }

      protected function onMouseMove(event:MouseEvent):void {
         var hscrollnum:Number = 0;
         var vscrollnum:Number = 0;

         if (!event.buttonDown)
            return;

         if (horizontalScrollBar)
            hscrollnum = horizontalScrollBar.scrollPosition;

         if (verticalScrollBar)
            vscrollnum = verticalScrollBar.scrollPosition;

         //创建Edge
         if (getBehaviorType() == GraphicalBehavior.Behavior_CreateEdge) {
            //这些移动都要通过改变连线当中的bend的位置实现而不能只是修改连线上某些点坐标
            currentDrawingEdge.setTargetBend(this.globalToLocal(new Point(event.stageX, event.stageY)).x + hscrollnum, this.globalToLocal(new Point(event.stageX, event.stageY)).y + vscrollnum);
            currentDrawingEdge.invalidateDisplayList();
            return;
         }

         //移动Edge Anchor
         if (getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Target) {
            currentDrawingEdge.setTargetBend(this.globalToLocal(new Point(event.stageX, event.stageY)).x + hscrollnum, this.globalToLocal(new Point(event.stageX, event.stageY)).y + vscrollnum);
            currentDrawingEdge.invalidateDisplayList();
            return;
         }
         if (getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Source) {
            currentDrawingEdge.setSourceBend(this.globalToLocal(new Point(event.stageX, event.stageY)).x + hscrollnum, this.globalToLocal(new Point(event.stageX, event.stageY)).y + vscrollnum);
            currentDrawingEdge.invalidateDisplayList();
            return;
         }

         //假如是Marquee
         if (IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType == GraphicalBehavior.Behavior_Marquee) {
            var maskLayer:Layer = this.getLayer(GraphicalViewport.LAYER_MASK);
            var currentMoustPoint:Point = maskLayer.globalToLocal(new Point(event.stageX, event.stageY));
            if (mask_cvs.visible) {
               mask_cvs.width = currentMoustPoint.x - mask_cvs.x;
               mask_cvs.height = currentMoustPoint.y - mask_cvs.y;

               checkMaskHit(this.getLayer(GraphicalViewport.LAYER_PRIMARY), event.stageX, event.stageY);
            }
            return;
         }

         //获取当前选中的对象
         var sm:SelectionManager = IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager();
         var numSelections:int = sm.numSelected;
         if (numSelections == 0) {
            return;
         }

         var substituteFigures:Array = this.getLayer(GraphicalViewport.LAYER_DRAGGING).getChildren();

         //判断是否是在操作LaneFigure
         var movingLaneFigure:Boolean = false;
         if (substituteFigures.length == 1 && IDraggingFigure(substituteFigures[0]).getOriginalFigure() is ILaneFigure) {
            movingLaneFigure = true;
         }


         //newPointGAP
         //记录 Figure顶点 与 长宽的位移差.
         var newPointGAP_x:Number = 0;
         var newPointGAP_y:Number = 0;
         var newPointGAP_width:Number = 0;
         var newPointGAP_height:Number = 0;

         //记录Lane拖动过程中的目标
         //var targetLocation:int = 0;

         //测量鼠标位移

         if (isResizingRight) {
            newPointGAP_width = (new Point(event.stageX, event.stageY)).x - this.originalPosition.x;
            wasResized = true;
         }
         if (isResizingDown) {
            newPointGAP_height = (new Point(event.stageX, event.stageY)).y - this.originalPosition.y;
            wasResized = true;
         }
         if (isResizingLeft) {
            newPointGAP_width = this.originalPosition.x - (new Point(event.stageX, event.stageY)).x;
            newPointGAP_x = (new Point(event.stageX, event.stageY)).x - this.originalPosition.x
            wasResized = true;
               //wasMoved = true;
         }
         if (isResizingUp) {
            newPointGAP_height = this.originalPosition.y - (new Point(event.stageX, event.stageY)).y;
            newPointGAP_y = (new Point(event.stageX, event.stageY)).y - this.originalPosition.y;
            wasResized = true;
         }
         if (isMoving) {
            wasMoved = true;
            if (!movingLaneFigure) {
               newPointGAP_x = (new Point(event.stageX, event.stageY)).x - this.originalPosition.x
               newPointGAP_y = (new Point(event.stageX, event.stageY)).y - this.originalPosition.y;
            }
            else {
               var tempLane:IDraggingFigure = IDraggingFigure(substituteFigures[0]);
               var originalLaneFigure:ILaneFigure = IDraggingFigure(substituteFigures[0]).getOriginalFigure() as ILaneFigure;

               //选出当前鼠标经过的LaneFigure
               var laneLayer:LaneLayer = LaneLayer(getLayer(GraphicalViewport.LAYER_LANE));
               var laneFigure:ILaneFigure = laneLayer.findLane(event.stageX, event.stageY) as ILaneFigure;
               if (laneFigure == null) {
                  //放置最后
                  targetLocationLaneFigure = null;
               }
               else {
                  targetLocationLaneFigure = laneFigure;
                  var tmpP:Point = IGraphicalViewer(this.getViewer()).getViewport().globalToLocal(new Point(event.stageX, event.stageY));
                  //放置鼠标所对应的
                  if (laneLayer.laneType == DiagramConstant.LANE_TYPE_HORIZONTAL) {
                     if (tmpP.y >= laneFigure.y + laneFigure.height / 2) {
                        targetLocationLaneFigure_before = false;
                     }
                     else {
                        targetLocationLaneFigure_before = true;
                     }
                  }
                  else {
                     if (tmpP.x >= laneFigure.x + laneFigure.width / 2) {
                        targetLocationLaneFigure_before = false;
                     }
                     else {
                        targetLocationLaneFigure_before = true;
                     }
                  }
               }
            }
         }

         if (isMoving) {
            wasMoved = true;
         }

         if (wasMoved || wasResized) {
            for each (var selection:IDraggingFigure in substituteFigures) {
               callLater(resizeMove, [selection, newPointGAP_x, newPointGAP_y, newPointGAP_width, newPointGAP_height]);
            }
         }

         this.originalPosition.x = event.stageX;
         this.originalPosition.y = event.stageY;
         event.updateAfterEvent();
      }

      protected function resizeMove(figure:IDraggingFigure, newPointGAP_x:Number, newPointGAP_y:Number, newPointGAP_width:Number, newPointGAP_height:Number):void {
         var currentZoomNum:Number = IGraphicalViewer(this.getViewer()).getZoomCurrentNumber();
         newPointGAP_x = newPointGAP_x / currentZoomNum;
         newPointGAP_y = newPointGAP_y / currentZoomNum;
         newPointGAP_width = newPointGAP_width / currentZoomNum;
         newPointGAP_height = newPointGAP_height / currentZoomNum;

         figure.visible = true;
         if (figure.getOriginalFigure() is ILaneFigure) {
            //此时需要判断Lane模式
            if (wasMoved) {
               var laneLayer:LaneLayer = LaneLayer(getLayer(GraphicalViewport.LAYER_LANE));
               if (targetLocationLaneFigure == null) {
                  if (laneLayer.laneType == DiagramConstant.LANE_TYPE_HORIZONTAL) {
                     figure.y = laneLayer.getLastLane().y + laneLayer.getLastLane().height;
                  }
                  else {
                     figure.x = laneLayer.getLastLane().x + laneLayer.getLastLane().width;
                  }
               }
               else {
                  if (targetLocationLaneFigure_before) {
                     if (laneLayer.laneType == DiagramConstant.LANE_TYPE_HORIZONTAL) {
                        figure.y = targetLocationLaneFigure.y;
                     }
                     else {
                        figure.x = targetLocationLaneFigure.x;
                     }
                  }
                  else {
                     if (laneLayer.laneType == DiagramConstant.LANE_TYPE_HORIZONTAL) {
                        figure.y = targetLocationLaneFigure.y + targetLocationLaneFigure.height;
                     }
                     else {
                        figure.x = targetLocationLaneFigure.x + targetLocationLaneFigure.width;
                     }
                  }
               }
            }
            else {
               figure.y = figure.y + newPointGAP_y;
               figure.x = figure.x + newPointGAP_x;
               figure.width = figure.width + newPointGAP_width;
               figure.height = figure.height + newPointGAP_height;
            }
         }
         else {
            var tmpRootFigure:GraphicalViewport = IGraphicalViewer(this.getViewer()).getViewport() as GraphicalViewport;
            //X限制
            if (figure.x + newPointGAP_x <= 1) {
               figure.x = 1;
            }
            else if (figure.x + figure.width + newPointGAP_x >= tmpRootFigure.width) {
               this.refreshScrollBar(figure.x + figure.width + newPointGAP_x, tmpRootFigure.height, newPointGAP_x);
            }
            else {
               figure.x = figure.x + newPointGAP_x;
            }

            //Y限制
            if (figure.y + newPointGAP_y <= 1) {
               figure.y = 1;
            }
            else if (figure.y + figure.height + newPointGAP_y >= tmpRootFigure.height) {
               this.refreshScrollBar(tmpRootFigure.width, figure.y + figure.height + newPointGAP_y, newPointGAP_y);
            }
            else {
               figure.y = figure.y + newPointGAP_y;
            }

            figure.width = figure.width + newPointGAP_width;
            if (figure.width < figure.minWidth)
               figure.width = figure.minWidth;
            figure.height = figure.height + newPointGAP_height;
            if (figure.height < figure.minHeight)
               figure.height = figure.minHeight;
         }
      }

      protected function onMouseUp(event:MouseEvent):void {
         var sm:SelectionManager = IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager();

         var targetFigure:IFigure = findFigureAt(event.stageX, event.stageY);
         if (targetFigure is ILabelFigure) {
            targetFigure = targetFigure.parent as IFigure;
         }

         //移动Edge Source Anchor之后
         if (getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Source || getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Target) {
            currentDrawingEdge.clean();

            //加入放在target node上.
            if (targetFigure is ISelection && targetFigure is INodeFigure) {
               //创建edge
               sm.setSelected(ISelection(targetFigure));
               var anchorType:String = "";

               var targetPoint:Point;
               if (getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Source) {
                  anchorType = DiagramConstant.EDGE_ANCHOR_LOCATION_SOURCE;
                  targetPoint = new Point(currentDrawingEdge.getSourceBend().x, currentDrawingEdge.getSourceBend().y);
               }
               else {
                  anchorType = DiagramConstant.EDGE_ANCHOR_LOCATION_TARGET;
                  targetPoint = new Point(currentDrawingEdge.getTargetBend().x, currentDrawingEdge.getTargetBend().y);
               }
               handleMoveEdgeAnchor(this.currentMovingOriginalEdgeFigure, targetFigure as INodeFigure, targetPoint, anchorType);
            }

            currentMovingOriginalEdgeFigure = null;
            setBehaviorType(GraphicalBehavior.Behavior_Select);
            return;
         }

         //移动Edge Target Anchor之后
         if (getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Target) {
            currentDrawingEdge.clean();
            return;
         }

         if (getBehaviorType() == GraphicalBehavior.Behavior_CreateEdge) {

            //加入放在target node上.
            if (targetFigure is ISelection && targetFigure is INodeFigure) {
               //创建edge
               sm.setSelected(ISelection(targetFigure));

               var sourcePoint:Point = new Point(currentDrawingEdge.getSourceBend().x, currentDrawingEdge.getSourceBend().y);
               targetPoint = new Point(currentDrawingEdge.getTargetBend().x, currentDrawingEdge.getTargetBend().y);
               handleCreateEdge(currentDrawingEdge.source, sourcePoint, targetFigure as INodeFigure, targetPoint, getBehaviorData());
            }
            currentDrawingEdge.graphics.clear();
            currentDrawingEdge.visible = false;
            return;
         }
         else {
            setBehaviorType(GraphicalBehavior.Behavior_Select);
            if (mask_cvs.visible) {
               mask_cvs.width = 0;
               mask_cvs.height = 0;
               mask_cvs.x = -1;
               mask_cvs.y = -1;
               mask_cvs.visible = false;
            }
         }

         if (this.wasMoved || wasResized) {
            var nodes:Array = new Array();
            //如果移动是Lane，则需要处理相关影响的其他Lane，做相应的上移或者下移.
            var substituteFigures:Array = this.getLayer(GraphicalViewport.LAYER_DRAGGING).getChildren();
            if (substituteFigures.length == 1 && IDraggingFigure(substituteFigures[0]).getOriginalFigure() is ILaneFigure) {
               //targetLocationLaneFigure
               //targetLocationLaneFigure_before
               var tempLane:IDraggingFigure = IDraggingFigure(substituteFigures[0]);
               var originalLaneFigure:ILaneFigure = IDraggingFigure(substituteFigures[0]).getOriginalFigure() as ILaneFigure;

               var laneLayer:LaneLayer = LaneLayer(getLayer(GraphicalViewport.LAYER_LANE));
               if (wasMoved) {
                  var lanes:ArrayCollection = laneLayer.getSortedLanes();
                  if (targetLocationLaneFigure == null) {
                     handleMoveLaneFigure(originalLaneFigure, lanes.length);
                  }
                  else {
                     if (targetLocationLaneFigure_before) {
                        handleMoveLaneFigure(originalLaneFigure, lanes.getItemIndex(targetLocationLaneFigure));
                     }
                     else {
                        handleMoveLaneFigure(originalLaneFigure, lanes.getItemIndex(targetLocationLaneFigure) + 1);
                     }
                  }
               }
               else {
                  handleResizeLaneFigure(originalLaneFigure, tempLane.x - originalLaneFigure.x, tempLane.y - originalLaneFigure.y, tempLane.width - IVertexFigure(originalLaneFigure).draggingWidth, tempLane.height - IVertexFigure(originalLaneFigure).draggingHeight);
               }
            }
            else {
               for each (var df:IDraggingFigure in substituteFigures) {
                  df.getOriginalFigure().refreshFigureWithSubstitute(df);
               }
               for each (var ifs:ISelection in sm.selecteds) {
                  if (ifs is IVertexFigure || ifs is IArtifactFigure) {
                     nodes.push(ifs);
                  }
               }
            }

            handleMoveNodes(nodes);

            //刷新ScrollBar
            //判断当前Drag对象是否已经到达边线.
            var maxRightLocation:Number = 0;
            var maxBottomLocation:Number = 0;
            for each (var selection:IFigure in sm.selecteds) {
               if (selection is IVertexFigure) {
                  if (maxRightLocation < selection.x + selection.width) {
                     maxRightLocation = selection.x + selection.width;
                  }
                  if (maxBottomLocation < selection.y + selection.height) {
                     maxBottomLocation = selection.y + selection.height;
                  }
               }
            }
            //未来将refreshScrollBar功能移交mouseUp中.
            this.refreshScrollBar(maxRightLocation, maxBottomLocation);
         }

         this.isMoving = false;
         this.wasMoved = false;
         this.wasResized = false;
         isResizingDown = false;
         isResizingLeft = false;
         isResizingRight = false;
         isResizingUp = false;
         cursorManager.removeCursor(cursorManager.currentCursorID);

         //删除所有替身图形.
         targetLocationLaneFigure = null;
         targetLocationLaneFigure_before = false;
         this.getLayer(GraphicalViewport.LAYER_DRAGGING).removeAllChildren();

         event.stopPropagation();
      }

      protected function onMouseOut(event:MouseEvent):void {
         var sm:SelectionManager = IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager();

         //移动Edge Source Anchor之后
         if (getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Source || getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Target) {
            currentDrawingEdge.clean();

            currentMovingOriginalEdgeFigure = null;
            setBehaviorType(GraphicalBehavior.Behavior_Select);
            dispatchEvent(new GraphicalBehaviorEvent(GraphicalBehaviorEvent.BEHAVIOR_CHANGED, true));
            return;
         }

         //移动Edge Target Anchor之后
         if (getBehaviorType() == GraphicalBehavior.Behavior_MoveEdgeAnchor_Target) {
            currentDrawingEdge.clean();
            return;
         }
         
         if (getBehaviorType() == GraphicalBehavior.Behavior_CreateEdge) {
            if (currentDrawingEdge) {
               currentDrawingEdge.graphics.clear();
               currentDrawingEdge.visible = false;
               return;
            }
         }
         else {
            setBehaviorType(GraphicalBehavior.Behavior_Select);
            dispatchEvent(new GraphicalBehaviorEvent(GraphicalBehaviorEvent.BEHAVIOR_CHANGED, true));
            if (mask_cvs.visible) {
               mask_cvs.x = -1;
               mask_cvs.y = -1;
               mask_cvs.width = 0;
               mask_cvs.height = 0;
               mask_cvs.visible = false;
            }
         }

         if (this.wasMoved || wasResized) {
            var nodes:Array = new Array();
            //如果移动是Lane，则需要处理相关影响的其他Lane，做相应的上移或者下移.
            var substituteFigures:Array = this.getLayer(GraphicalViewport.LAYER_DRAGGING).getChildren();
            if (substituteFigures.length == 1 && IDraggingFigure(substituteFigures[0]).getOriginalFigure() is ILaneFigure) {
               //targetLocationLaneFigure
               //targetLocationLaneFigure_before
               var tempLane:IDraggingFigure = IDraggingFigure(substituteFigures[0]);
               var originalLaneFigure:ILaneFigure = IDraggingFigure(substituteFigures[0]).getOriginalFigure() as ILaneFigure;

               var laneLayer:LaneLayer = LaneLayer(getLayer(GraphicalViewport.LAYER_LANE));

               if (wasMoved) {

                  var lanes:ArrayCollection = laneLayer.getSortedLanes();
                  if (targetLocationLaneFigure == null) {
                     handleMoveLaneFigure(originalLaneFigure, lanes.length);
                  }
                  else {
                     if (targetLocationLaneFigure_before) {
                        handleMoveLaneFigure(originalLaneFigure, lanes.getItemIndex(targetLocationLaneFigure));
                     }
                     else {
                        handleMoveLaneFigure(originalLaneFigure, lanes.getItemIndex(targetLocationLaneFigure) + 1);
                     }
                  }

               }
               else {
                  handleResizeLaneFigure(originalLaneFigure, tempLane.x - originalLaneFigure.x, tempLane.y - originalLaneFigure.y, tempLane.width - IVertexFigure(originalLaneFigure).draggingWidth, tempLane.height - IVertexFigure(originalLaneFigure).draggingHeight);
               }
            }
            else {
               for each (var df:IDraggingFigure in substituteFigures) {
                  df.getOriginalFigure().refreshFigureWithSubstitute(df);
               }
               for each (var ifs:ISelection in sm.selecteds) {
                  if (ifs is IVertexFigure || ifs is IArtifactFigure) {
                     nodes.push(ifs);
                  }
               }
            }

            handleMoveNodes(nodes);

            //刷新ScrollBar
            //判断当前Drag对象是否已经到达边线.
            var maxRightLocation:Number = 0;
            var maxBottomLocation:Number = 0;
            for each (var selection:IFigure in sm.selecteds) {
               if (selection is IVertexFigure) {
                  if (maxRightLocation < selection.x + selection.width) {
                     maxRightLocation = selection.x + selection.width;
                  }
                  if (maxBottomLocation < selection.y + selection.height) {
                     maxBottomLocation = selection.y + selection.height;
                  }
               }
            }

            //未来将refreshScrollBar功能移交mouseUp中.
            this.refreshScrollBar(maxRightLocation, maxBottomLocation);
         }

         this.isMoving = false;
         this.wasMoved = false;
         this.wasResized = false;
         isResizingDown = false;
         isResizingLeft = false;
         isResizingRight = false;
         isResizingUp = false;
         if (cursorManager.currentCursorID != CursorManagerExt.currentEdgeCursorID)
            cursorManager.removeCursor(cursorManager.currentCursorID);

         //删除所有替身图形.
         targetLocationLaneFigure = null;
         targetLocationLaneFigure_before = false;
         this.getLayer(GraphicalViewport.LAYER_DRAGGING).removeAllChildren();
      }

      protected function onMouseOver(event:MouseEvent):void {
      }

      /**
       * MouseClick 默认不做任何响应，由分散的MouseDown和MouseUp组合响应.
       * @param event
       *
       */
      protected function onMouseClick(event:MouseEvent):void {
         //获取鼠标所放置位置的Figure图形.
         var targetFigure:IFigure = findFigureAt(event.stageX, event.stageY);
         setFocus();
         if (targetFigure is ILabelFigure && !(targetFigure is AnnotationLabelFigure)) {
            if (ILabelFigure(targetFigure).isSelected) {
               var labelLayer:LabelLayer = getLayer(GraphicalViewport.LAYER_LABEL) as LabelLayer;
               labelLayer.editText(ILabelFigure(targetFigure));
            }
            else {
               ILabelFigure(targetFigure).isSelected = true;
            }
         }

         if (targetFigure is ISelection) {
            pushFigureIntoSelectionManager(ISelection(targetFigure), event);
         }
      }

      /**
       * 获取选中的 Figure，然后转移到对应的 EditPart操作上.
       * @param event
       *
       */
      protected function onMouseDoubleClick(event:MouseEvent):void {
         //获取鼠标所放置位置的Figure图形.
         var targetFigure:IFigure = findFigureAt(event.stageX, event.stageY);
         handleFigureProperty(targetFigure);
      }

      private function handleFigureProperty(targetFigure:IFigure):void {
         var labelLayer:LabelLayer = getLayer(GraphicalViewport.LAYER_LABEL) as LabelLayer;
         if (targetFigure is IArtifactFigure) {
            //暂时不做响应
            labelLayer.editText(ArtifactFigure(targetFigure).getLabelFigure());
            return;
         }

         if (targetFigure is AnnotationLabelFigure) {
            labelLayer.editText(ILabelFigure(targetFigure));
            return;
         }

         if (targetFigure is ILabelFigure) {
            var containerFigure:IFigure = ILabelFigure(targetFigure).getContainer();
            handleFigureDBClick(containerFigure);
            return;
         }
         handleFigureDBClick(targetFigure);
      }

      private function handleFigureDBClick(targetFigure:IFigure):void {
         if (targetFigure is INodeFigure) {
            //获取相应的EditPart，进行操作.
            handleMouseDoubleClick_onNode(INodeFigure(targetFigure));
         }

         if (targetFigure is IEdgeFigure) {
            handleMouseDoubleClick_onEdge(IEdgeFigure(targetFigure));
         }

         if (targetFigure is IContainerFigure) {
            handleMouseDoubleClick_onPool(IContainerFigure(targetFigure));
         }
      }

//=========================================================================
//
// 提供基鼠标双击处理处理
//
//=========================================================================

      public function handleMouseDoubleClick_onNode(targetFigure:INodeFigure):void {

      }

      public function handleMouseDoubleClick_onPool(targetFigure:IContainerFigure):void {

      }

      public function handleMouseDoubleClick_onEdge(targetFigure:IEdgeFigure):void {

      }


//=========================================================================
//
// 提供基handle处理
//
//=========================================================================

      public function handleCreateEdge(source:INodeFigure, sourcePoint:Point, target:INodeFigure, targetPoint:Point, behaviorData:*):void {

      }

      public function handleMoveEdgeAnchor(edgeFigure:IEdgeFigure, target:INodeFigure, targetPoint:Point, anchorType:String):void {
         var edgeEP:EdgeEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(edgeFigure) as EdgeEditPart;
         var tep:IEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(target);

         var edge:Edge = edgeEP.model as Edge;
         var targetNode:Node = tep.model as Node;

         var command:ICommand = null;
         if (anchorType == DiagramConstant.EDGE_ANCHOR_LOCATION_SOURCE) {
            edge.sourcePoint = targetPoint;
            command = edgeEP.createMoveSourceAnchorCommand(targetNode);
         }
         else {
            edge.targetPoint = targetPoint;
            command = edgeEP.createMoveTargetAnchorCommand(targetNode);
         }
         IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().push(command);
      }

      public function handleMoveNodes(nodes:Array /*IVertexFigure*/):void {
         var command:CompoundCommand = new CompoundCommand();
         for each (var nodeFigure:IVertexFigure in nodes) {
            var aep:IEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(nodeFigure);
            if (aep.model is Vertex) {
               var vertex:Vertex = Vertex(aep.model);
               var mrCommand:ICommand = aep.createMoveResizeCommand(nodeFigure.x - vertex.x, nodeFigure.y - vertex.y, nodeFigure.width - vertex.width, nodeFigure.height - vertex.height);
               command.addCommand(mrCommand);
            }
         }
         IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().push(command);
      }

      public function handleMoveLaneFigure(laneFigure:ILaneFigure, toLocation:int):void {
      }

      public function handleResizeLaneFigure(laneFigure:ILaneFigure, g_x:Number, g_y:Number, g_width:Number, g_height:Number):void {
      }

//=========================================================================
//
// 提供基础的一些底层服务
//
//=========================================================================

      public function setBehaviorType(behavior:int):void {
         IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType = behavior;
      }

      public function getBehaviorType():int {
         return IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorType;
      }

      public function getBehaviorData():* {
         return IEditor(Workbench.instance.currentEditor).getEditDomain().behaviorData;
      }

      /**
       * 获取最顶端的RootEditPart对象.
       * @return
       *
       */
      public function getRootEditPart():IEditPart {
         var pl:PrimaryLayer = getLayer(GraphicalViewport.LAYER_PRIMARY) as PrimaryLayer;
         var root:IFigure = pl.getRootFigure();
         return IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(root);
      }

      /**
       * 获取鼠标所放置位置的Figure图形
       * @param stageX
       * @param stageY
       * @return
       *
       */
      protected function findFigureAt(stageX:Number, stageY:Number):IFigure {
         //首先从Handles Layer层查找
         //首先从Connetion Layer层查找
         //先找Handle
         var targetFigure:IFigure = getLayer(GraphicalViewport.LAYER_HANDLES).findFigureAt(stageX, stageY);
         if (!(targetFigure is Layer)) {
            return targetFigure;
         }
         //再找connection
         targetFigure = getLayer(GraphicalViewport.LAYER_CONNECTION).findFigureAt(stageX, stageY);

         //再找Lane
         if (targetFigure is Layer) {
            targetFigure = getLayer(GraphicalViewport.LAYER_LANE).findFigureAt(stageX, stageY);
         }
         //再找activity
         if (targetFigure is Layer) {
            targetFigure = getLayer(GraphicalViewport.LAYER_PRIMARY).findFigureAt(stageX, stageY);
         }

         if (targetFigure is Layer) {
            targetFigure = getLayer(GraphicalViewport.LAYER_PRIMARY);
         }
         return targetFigure;
      }

      /**
       * 获取当前鼠标所放置的Lane对象.
       * @param stageX
       * @param stageY
       * @return
       *
       */
      protected function findFigureAtLaneLayer(stageX:Number, stageY:Number):IFigure {
         return null;
      }

      /**
       * 检查Mask在移动过程中所碰到的对象.
       * @param uiComponent
       * @param stageX
       * @param stageY
       *
       */
      protected function checkMaskHit(uiComponent:UIComponent, stageX:Number, stageY:Number):void {
         //将当前选中Figure放入SelectionManager
         var sm:SelectionManager = IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager();
         var currentMousePoint:Point = uiComponent.globalToLocal(new Point(stageX, stageY));

         if (uiComponent is IContainerFigure) {
            var tmp_children:Array = Container(uiComponent).getChildren();
            for each (var child:UIComponent in tmp_children) {
               if (child == null)
                  return;
               if (child is IContainerFigure) {
                  checkMaskHit(child, stageX, stageY);
               }
               else if (child is ISelection) {
                  if (this.mask_cvs.hitTestObject(UIComponent(child))) {
                     sm.addSelected(ISelection(child));
                  }
                  else {
                     if (UIComponent(child).getBounds(uiComponent).contains(currentMousePoint.x, currentMousePoint.y)) {
                        sm.addSelected(ISelection(child));
                     }
                     else {
                        sm.removeSelected(ISelection(child));
                     }
                  }
               }
            }
         }
      }

      /**
       * 将选择的Figure放入SelectionManager.
       * @param targetFigure
       * @param event
       *
       */
      public function pushFigureIntoSelectionManager(targetFigure:ISelection, event:MouseEvent = null):void {

         //将当前选中Figure放入SelectionManager
         var sm:SelectionManager = IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager();

         var tmpLaneFigure:ISelection = null;
         if (targetFigure is ILaneFigure) {
            sm.setSelected(ISelection(targetFigure));
            DepthManager.bringToTop(targetFigure as DisplayObject);
         }
         else {
            //假如是按住Shift Key，则是多选.
            if (event && event.shiftKey) {
               //将之前选择的 LaneFigure移除，根据当前的算法，只可能存在一个LaneFigure已经被选中的可能.
               for each (var tmpSF:ISelection in sm.selecteds) {
                  if (tmpSF is ILaneFigure) {
                     tmpLaneFigure = tmpSF;
                     break;
                  }
               }
               if (tmpLaneFigure != null) {
                  sm.removeSelected(tmpLaneFigure);
               }

               if (ISelection(targetFigure).isSelected)
                  sm.removeSelected(ISelection(targetFigure));
               else
                  sm.addSelected(ISelection(targetFigure));
            }
            else {
               sm.setSelected(ISelection(targetFigure));
               DepthManager.bringToTop(targetFigure as DisplayObject);
            }
         }

      }

      /**
       * 获取当前Editor中已经选择的对象.
       * @return
       *
       */
      protected function getSelections():Array {
         return IEditor(Workbench.instance.currentEditor).getEditDomain().getSelectionManager().selecteds;
      }

      /**
       * 更具Layer名称获取Layer.
       * @param name
       * @return
       *
       */
      protected function getLayer(name:String):Layer {
         return IGraphicalViewer(getViewer()).getViewport().getLayer(name);
      }

      protected function refreshScrollBar(maxRightLocation:Number, maxBottomLocation:Number, moveLength:Number = 100):void {
         var tmpRootFigure:GraphicalViewport = IGraphicalViewer(getViewer()).getViewport() as GraphicalViewport;
         if (maxRightLocation > tmpRootFigure.width - 30) {
            tmpRootFigure.width = tmpRootFigure.width + moveLength;
            if (Container(tmpRootFigure.parent).horizontalScrollBar) {
               var horizontalPosition_now:Number = Container(tmpRootFigure.parent).horizontalScrollBar.scrollPosition;
               Container(tmpRootFigure.parent).horizontalScrollBar.scrollPosition = horizontalPosition_now + moveLength;
            }
         }
         if (maxBottomLocation > tmpRootFigure.height - 30) {
            tmpRootFigure.height = tmpRootFigure.height + moveLength;
            if (Container(tmpRootFigure.parent).verticalScrollBar) {
               var vscrollPosition_now:Number = Container(tmpRootFigure.parent).verticalScrollBar.scrollPosition;
               Container(tmpRootFigure.parent).verticalScrollPosition = vscrollPosition_now + moveLength;
            }
         }
      }

      protected function onModelSelect(evt:ModelEvent):void {
         var model:Model = evt.model;
         var eP:IEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(model);
         if (eP != null)
            pushFigureIntoSelectionManager(eP.figure as ISelection);
      }
   }
}